const reg_mark = /^(.+?)\s/ //开头# 部分
const reg_sharp = /^\#/ //开头#
const reg_ul = /^\-/ //开头-
const reg_ol = /^\d/ //开头数字

function createTree(mdArr) {
    let htmlPool = new Map()
    let lastMark = '' //上一行的标记类型 #,-或者其他的
    let tagMap = {} //标记数

    mdArr.forEach(mdFragement => {
        const matched = mdFragement.match(reg_mark)
        if (matched) {
            const mark = matched[1]
            const input = matched['input']

            if (reg_sharp.test(mark)) {
                const tag = `h${mark.length}`
                const tagContent = input.replace(reg_mark, '')

                if (lastMark === mark) {
                    htmlPool.get(`${tag}-${tagMap[tag]}`).tags.push(`<${tag}>${tagContent}</${tag}>`)
                } else {
                    tagMap[tag] ? tagMap[tag] = tagMap[tag] + 1 : tagMap[tag] = 1
                    htmlPool.set(`${tag}-${tagMap[tag]}`, {
                        type: 'singer',
                        tags: [`<${tag}>${tagContent}</${tag}>`]
                    })
                    lastMark = mark
                }


            }

            if (reg_ul.test(mark)) {
                const tag = 'li'
                const tagContent = input.replace(reg_mark, '')

                if(reg_ul.test(lastMark)) {
                    htmlPool.get(`ul-${tagMap[tag]}`).tags.push(`<${tag}>${tagContent}</${tag}>`)
                } else {
                    tagMap[tag] ? tagMap[tag] = tagMap[tag] + 1 : tagMap[tag] = 1
                    htmlPool.set(`ul-${tagMap[tag]}`, {
                        type: 'wrap',
                        tags: [`<${tag}>${tagContent}</${tag}>`]
                    })
                    lastMark = mark
                }
            }

            if (reg_ol.test(mark)) {
                const tag = 'li'
                const tagContent = input.replace(reg_mark, '')

                if(reg_ol.test(lastMark)) {
                    htmlPool.get(`ol-${tagMap[tag]}`).tags.push(`<${tag}>${tagContent}</${tag}>`)
                } else {
                    tagMap[tag] ? tagMap[tag] = tagMap[tag] + 1 : tagMap[tag] = 1
                    htmlPool.set(`ol-${tagMap[tag]}`, {
                        type: 'wrap',
                        tags: [`<${tag}>${tagContent}</${tag}>`]
                    })
                    lastMark = mark
                }
            }
        }
    });
    console.log(htmlPool)
}

function compilerHtml(mdArr) {
    const htmlPool = createTree(mdArr)
}

module.exports = {
    compilerHtml
}